//
//  DCImageChatTableViewCell.m
//  xmppTest
//
//  Created by striby on 15/12/31.
//  Copyright © 2015年 com.dc. All rights reserved.
//

#import "DCImageChatTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation DCImageChatTableViewCell

-(void)loadBaseUI
{
    [super loadBaseUI];
    
    _contentImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
//    _contentImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.constant =0;
    

    
    [self.baseContentView addSubview:self.contentImageView];
}

-(void)displayCell
{
    
//    
    if(_messageDirection==DCChatDirectionLeft)
    {
        self.userIconImageView.frame = CGRectMake(baseCellMargin, baseCellMargin, userIconWidth, userIconWidth);  
    }
    else
    {
        self.userIconImageView.frame = CGRectMake(DCDeviceWidth-baseCellMargin-userIconWidth, baseCellMargin, userIconWidth, userIconWidth);
        
    }

    if (self.isDisplayUserNickName) {
        self.nickNameLabel.hidden=NO;
        CGSize size = [self.nickNameLabel.text sizeWithAttributes:@{NSFontAttributeName:self.nickNameLabel.font}];
        
        if (_messageDirection==DCChatDirectionLeft) {
            
            [self.nickNameLabel setFrame:CGRectMake(self.userIconImageView.frame.origin.x+self.userIconImageView.frame.size.width+self.constant+baseCellMargin, self.userIconImageView.frame.origin.y, size.width, size.height)];
        }
        else
        {
            [self.nickNameLabel setFrame:CGRectMake(self.userIconImageView.frame.origin.x-self.constant-size.width, self.userIconImageView.frame.origin.y, size.width, size.height)];
        }
        
    }
    else
    {
        self.nickNameLabel.hidden=YES;
    }
  
    [self configImageViewFrame];

     [self.baseContentView setFrame:CGRectMake(0, self.isDisplaySendTime?(sendTimeBackgroundHeight+2*baseCellMargin):baseCellMargin*2,DCDeviceWidth, self.contentImageView.frame.size.height+baseCellMargin+self.contentImageView.frame.origin.y)];
//
//    
    if (self.isDisplaySendTime) {
        
        CGSize size = [self.sendTimeLbl.text sizeWithAttributes:@{NSFontAttributeName:self.sendTimeLbl.font}];
        
        self.sendTimeBackgroundView.hidden=NO;
        self.sendTimeLbl.hidden=NO;
        
        [self.sendTimeBackgroundView setFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-(size.width+6))/2, baseCellMargin, size.width+6, sendTimeBackgroundHeight)];
        
        [self.sendTimeLbl setFrame:CGRectMake(2, 0, size.width+2, sendTimeBackgroundHeight)];
    }
    else
    {
        self.sendTimeBackgroundView.hidden=YES;
        self.sendTimeLbl.hidden=NO;
        
        
    }
}

-(void)configImageViewFrame
{
  
    CGFloat height = imageHieght;


    
    [self.contentImageView setFrame:
     CGRectMake(
                
                _messageDirection==DCChatDirectionLeft?self.userIconImageView.frame.origin.x+self.userIconImageView.frame.size.width+self.constant+baseCellMargin:self.userIconImageView.frame.origin.x-self.constant-(DCDeviceWidth/3)-baseCellMargin,
                
                self.isDisplayUserNickName==NO?self.userIconImageView.frame.origin.y:self.nickNameLabel.frame.origin.y+self.nickNameLabel.frame.size.height+baseCellMargin,
                
                self.contentImageView.bounds.size.width, height)];
    
}

-(CGFloat)cellHeight
{
    return self.baseContentView.frame.origin.y+self.baseContentView.frame.size.height+baseCellMargin+1;

}

-(CGFloat)heightFor:(XMPPMessageArchiving_Message_CoreDataObject *)messageObject
{
    
    XMPPMessage * message = messageObject.message;
    
    DDXMLElement * sendTime = [[messageObject.message elementsForName:@"sendTime"] firstObject];
    
    DCImageChatTableViewCell * cell = self;
    
    cell.isDisplaySendTime =YES;
    
    if (sendTime) {
        if (sendTime.stringValue) {
            cell.sendTimeLbl.text = sendTime.stringValue;
        }
    }
    
    
    
    
    XMPPJID * from ;
    if ([message elementForName:messageFrom]) {
        
        from = [XMPPJID jidWithString:[[message elementForName:messageFrom] stringValue]];
    }
    
    XMPPvCardTemp * card = [[DCXMPPTool shareInstance].xmppvCardTempModule vCardTempForJID:from!=nil?from:message.from shouldFetch:YES];
    
    if (messageObject.isOutgoing) {
        [cell setMessageDirection:DCChatDirectionRight];
        cell.isDisplayUserNickName=NO;
    }
    else
    {
        [cell setMessageDirection:DCChatDirectionLeft];
        cell.isDisplayUserNickName=YES;
        
        cell.nickNameLabel.text = card.nickname;
        
    }
    
    
    
    if (card.photo) {
        cell.userIconImageView.image=[UIImage  imageWithData:card.photo];
    }
    else
    {
        cell.userIconImageView.image = [UIImage imageNamed:@"defaultUserIcon"] ;
    }
    
    
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:message.body] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            float imageWidth = image.size.width/image.size.height*imageHieght;
            
            [self.contentImageView setFrame:CGRectMake(self.contentImageView.frame.origin.x, self.contentImageView.frame.origin.y, imageWidth, imageHieght)];
        }
    }];
    
    
    [cell displayCell];
    
    return [self cellHeight];
}

@end
